package com.boxing.dao;

import com.boxing.bean.User;
import com.boxing.bean.UserDesc;

public class UserDaoImpl extends BaseDao implements UserDao{

    /**
     * @return true 登录成功    false 登录失败
     */
    public boolean queryUser(User user){
        String sql = "select name, psd from t_user where name=? and psd=?";
        User user1 = queryOne(sql, User.class, user.getUser(), user.getPsd());
        System.out.println("====="+user1);
        return user1 != null && user1.getUser() != null;
    }

    @Override
    public boolean verifyUser(User user) {
        return queryUser(user);
    }

    /**
     * 查询用户名是否存在
     * @param userName
     * @return true 存在
     */
    @Override
    public boolean existUser(String userName) {
        String sql = "select name, psd from t_user where name=?";
        User user = queryOne(sql, User.class, userName);
        return user!= null && !user.getUser().isEmpty();
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @Override
    public boolean registerUser(User user) {
        String sql = "insert into t_user(name, psd) values(?,?)";
        String sqlDesc = "insert into t_user_desc(name, email) values(?,?)";
        int sqlDescEnd = insertData(sqlDesc, user.getUser(), user.getEmail());
        int sqlEnd = insertData(sql, user.getUser(), user.getPsd());
        return sqlEnd > 0 && sqlDescEnd > 0;
    }

    @Override
    public UserDesc getUser(String name) {
        String sql = "select name,sex,icon, email from t_user_desc where name=?";
        return queryOne(sql, UserDesc.class, name);
    }

    @Override
    public boolean editUser(UserDesc userDesc) {
        String sql = "update t_user_desc set email=?, sex=? where name=?";
        return insertData(sql, userDesc.getEmail(), userDesc.getSex(), userDesc.getUser())>0;
    }

    /**
     * 修改用户头像
     * @param userDesc
     * @return
     */
    @Override
    public boolean editIcon(UserDesc userDesc) {
        String sql = "update t_user_desc set icon=? where name=?";
        return insertData(sql, userDesc.getIcon(), userDesc.getUser())>0;
    }
}
